class MyPromise {
    constructor(executor) {
        this.state = 'pading'// 状态
        this.onFulfilledCallbacks = []
        this.onRejectedCallbacks = []
        const resolve = (value) => {
            if (this.state === 'pading') {
                this.state = 'fulfilled'
                //帮忙把then中的函数取出来执行
                this.onFulfilledCallbacks.forEach(fn => fn(value))

            }
        }
        const reject = (reason) => {
            if (this.state === 'pading') {
                //帮忙把catch中的函数取出来执行
                this.state = 'rejected'
                this.onRejectedCallbacks.forEach(fn => fn(reason))
            }

        }
        executor(resolve, reject)
    }
    then(onFulfilled, onRejected) {76
        //onFulfilled被存起来
        onFulfilled = typeof onFulfilled === 'function' ? onFulfilled : (value) => { value }
        //onRejected被存起来
        onRejected = typeof onRejected === 'function' ? onRejected : (reason) => { throw reason }
        // 默认返回一个promis对象
        const newPromise = new MyPromise((resolve, reject) => {
            if (this.state === 'fulfilled') {//onFulfilled要自己触发
                setTimeout(() => {
                    try {
                        const result = onFulfilled()
                        resolve(result)
                    } catch (error) {
                        reject(error)

                    }

                }, 0);
            }
            if (this.state === 'rejected') {//onRejected要自己触发
                setTimeout(() => {
                    try {
                        const result = onRejected()
                        resolve(result)
                    } catch (error) {
                        reject(error)
                    }
                })
            }
            if (this.state === 'pading') { //onFulfilled和onRejectde都要存起来
                this.onFulfilledCallbacks.push((value) => {
                    setTimeout(() => {
                        try {
                            const result = onFulfilled(value)
                            resolve(result)
                        } catch (error) {
                            reject(error)
                        }
                    }, 0)
                })
                this.onRejectedCallbacks.push((reason) => {
                    setTimeout(() => {
                        onRejected(reason)
                    })
                })
            }
        })

        return newPromise
    }
    static race(promises) {
        return new MyPromise((resolve, reject) => {
            for (let promise of promises) {
                promise.then(
                    (value) => {
                        resolve(value)
                    }, (reason) => {
                        reject(reason)
                    })
            }
        })
    }
    static all(promises){
        const result=[]
        let count=0
        return new MyPromise((resolve,reject)=>{
            for(let i=0;i<promises.length;i++){
                promises[i].then(
                    (value)=>{
                       result[i]=value
                       count++
                       if(count===promises.length){
                           resolve(result)
                       }
                    },(reason)=>{
                        reject(reason)
                    }
                )
            }
            
            //resolve()
            //reject()

        })
    }
    static any(promise){
        let count=0
        let errors=[]
        return new MyPromise((resolve,reject)=>{
            for(let i=0;i<promise.length;i++){
                promise[i].then(
                    (value)=>{
                    resolve(value)
                    },(reason)=>{
                        errors[i]=reason
                        count++
                        if(count===promise.length){
                            reject(errors)
                        }
                    }
                )
            }
        })
    }
    static resolve(value){
        return new MyPromise((resolve,reject)=>{
            resolve(value)
        })
    }
    static reject(reason){
        return new MyPromise((resolve,reject)=>{
            reject(reason)
        })
    }

    finally(callback){
        this.then(
        (value)=>{
            return MyPromise.resolve(callback()).then(()=>value)
        },
        (reason)=>{
            return MyPromise.resolve(callback()).then(()=>{
                throw reason
            })
        } 
    )
        //callback()


    }
    static allSettled(promises){
        return new MyPromise((resolve,reject)=>{
        const result=[]
        let count=0

        for(let i=0;i<promises.length;i++){
          promises[i]
          .then(()=>{
            result[i]={status:'fulfilled',value:value},
            (reason)=>{
            result[i]={status:'rejected',reason:reason}
          }
          }).finally(()=>{
            count++
            if(count===promises.length){
                resolve(result)
            }
          })
        }
         resolve(result)

        })

    }

}

let p = new MyPromise((resolve, reject) => {
    setTimeout(() => {
        resolve('ok')
        reject()

    }, 1000)
})
p.then((res) => {
    console.log('then成功', res)
})
